<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VGroup>
          <el-select v-model="form.input.type" style="width: 182px">
            <el-option label="采购单号" value="purchase_no"></el-option>
            <el-option label="1688订单号" value="platform_order_no"></el-option>
            <el-option label="计划单号" value="plan_no"></el-option>
            <el-option label="来源单号" value="source_no"></el-option>
            <el-option label="付款单号" value="payment_no"></el-option>
            <el-option label="跟踪号" value="tracking_no"></el-option>
            <el-option label="合同单号" value="contact_no"></el-option>
            <el-option label="备料单号" value="plan_no"></el-option>
            <el-option label="采购单号/SKU/备货PR" value="purchase_no_sku_source_no"></el-option>
          </el-select>
          <VBatchInput v-model="form.input.value" @search="query" class="w-60" />
        </VGroup>
        <VGroup>
          <el-select v-model="form.skuInfo.type" class="w-28">
            <el-option label="sku" value="sku"></el-option>
            <el-option label="赛盒SKU" value="sai_he_sku"></el-option>
            <el-option label="自定义sku" value="custom_sku"></el-option>
          </el-select>
          <VBatchInput v-model="form.skuInfo.value" @search="query" class="w-60" />
        </VGroup>
        <VSelect v-model="form.supplier_name" placeholder="供应商" type="supplierName" allow-create style="min-width: 200px" multiple collapse-tags clearable filterable />
        <VSelect v-model="form.purchase_audit_status" placeholder="订单状态" select :options="options.purchase_audit_status" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.order_source" placeholder="订单来源" :options="options.order_source" multiple collapse-tags style="min-width: 160px" />
        <VCascader v-model="form.department_name" placeholder="需求部门" type="department" multiple collapse-tags checkStrictly style="min-width: 160px" />
        <VSelect v-model="form.destination_warehouse" placeholder="发货仓库" type="warehouse156" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.purchase_type" placeholder="供应商类型" :options="options.purchase_type" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.payment_type" placeholder="付款方式" :options="options.payment_type" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.period_id" placeholder="结算方式" type="period" multiple collapse-tags style="min-width: 220px" />
        <VSelect v-model="form.has_combination" placeholder="是否有组合sku" :options="options.is_new" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.order_status" placeholder="1688订单状态" :options="options.order_status" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.trade_type" placeholder="1688交易方式" :options="options.trade_type" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.pay_type" placeholder="1688支付方式" :options="options.pay_type" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.country" placeholder="国家" :options="options.country" multiple collapse-tags style="min-width: 160px" />
        <VGroup>
          <el-select v-model="form.user.type" style="width: 130px">
            <el-option label="采购人员" value="purchase_user_id"></el-option>
            <el-option label="供应商负责人员" value="director_id"></el-option>
          </el-select>
          <VCascader v-model="form.user.value" placeholder="人员" multiple collapse-tags style="min-width: 160px" />
        </VGroup>
        <VSelect v-model="form.purchase_status" placeholder="采购状态" :options="options.purchase_status" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.prepare_type" placeholder="备货方式" :options="options.prepare_type" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_drawback" placeholder="是否退税" :options="options.is_drawback" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.company_id" placeholder="公司主体" :options="options.company_id" multiple collapse-tags style="min-width: 210px" />
        <VSelect v-model="form.is_change" placeholder="是否变更" :options="options.is_new" multiple collapse-tags style="min-width: 160px" />
        <VGroup>
          <el-select v-model="form.warehouse.type" class="w-28" @change="form.warehouse.value = []">
            <el-option label="需求仓平台" value="demand_warehouse"></el-option>
            <el-option label="需求仓库" value="plan_demand_warehouse"></el-option>
          </el-select>
          <VSelect v-if="form.warehouse.type === 'demand_warehouse'" v-model="form.warehouse.value" placeholder="请选择" :options="options.demand_warehouse" multiple collapse-tags
            style="min-width: 160px" />
          <VSelect v-else v-model="form.warehouse.value" placeholder="请选择" :options="options.plan_demand_warehouse" multiple collapse-tags style="min-width: 160px" />
        </VGroup>
        <VSelect v-model="form.finance_status" placeholder="财务状态" :options="options.finance_status" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_period_expect" placeholder="阿里账期预计逾期还款" :options="options.is_period_expect" multiple collapse-tags style="min-width: 200px" />
        <VSelect v-model="form.period_pay_status" placeholder="账期额度支付" :options="options.period_pay_status" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.purchase_label" placeholder="采购标签" :options="options.purchase_label" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_contract" placeholder="是否生成采购合同" :options="options.is_contract" style="width: 168px" />
        <VSelect v-model="form.is_complete" placeholder="是否完结" :options="options.is_complete" multiple collapse-tags style="min-width: 120px" />
        <VSelect v-model="form.complete_type" placeholder="完结类型" :options="options.complete_type" multiple collapse-tags style="min-width: 120px" />
        <VSelect v-model="form.platform" :options="options.platform" multiple collapse-tags style="min-width: 160px" placeholder="平台" />
        <VGroup>
          <el-select v-model="form.new.type" style="width: 130px">
            <el-option label="是否新品" value="is_new"></el-option>
            <el-option label="是否特采" value="is_special_purchase"></el-option>
          </el-select>
          <VSelect v-model="form.new.value" placeholder="请选择" :options="options.is_new" multiple collapse-tags style="min-width: 160px" />
        </VGroup>
        <VSelect v-model="form.is_dangerous" placeholder="是否危险品" :options="options.is_dangerous" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_cashier_reject" placeholder="出纳驳回" :options="options.is_cashier_reject" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_accord" placeholder="是否需求不满足" :options="options.is_accord" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.demand_form" :options="options.demand_form" multiple collapse-tags style="min-width: 160px" placeholder="备库需求单类型" />
        <VGroup>
          <el-select v-model="form.dateRange.type" style="width: 100px">
            <el-option label="下单时间" value="create_time"></el-option>
            <el-option label="最后审核时间" value="audit_time"></el-option>
            <el-option label="申请付款时间" value="apply_time"></el-option>
            <el-option label="付款时间" value="payment_time"></el-option>
            <el-option label="最后入库时间" value="inbound_time"></el-option>
            <el-option label="完结时间" value="complete_time"></el-option>
            <el-option label="SKU入库时间" value="sku_inbound_time"></el-option>
            <el-option label="需求时间" value="demand_time"></el-option>
          </el-select>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
        <VSelect v-model="form.is_custom_product" :options="options.is_custom_product" multiple collapse-tags filterable clearable style="min-width: 140px" placeholder="是否定制化" />
      </template>
      <template #toolbar_btns>
        <VButton auth="/purchase/manage/purchase/generate_purchase_contract" type="primary" @click="handleBatch('contract')"><svg-icon icon-class="sc" class="mr-1"
            style="font-size: 15px" />手动生成采购合同
        </VButton>
        <VButton auth="/purchase/manage/purchase/auto_generate_purchase_contract" type="primary" @click="handleBatch('auto_contract')"><svg-icon icon-class="sc" class="mr-1"
            style="font-size: 15px" />自动生成采购合同
        </VButton>
        <VButton auth="/purchase/manage/purchase/batch_return" @click="handleBatch('back')">批量退回</VButton>
        <VButton auth="/purchase/manage/purchase/payment_of_credit_limit" @click="handleBatch('periodPayment')">账期额度支付
        </VButton>
        <VButton auth="/purchase/manage/purchase/apply_for_a_refund" :loading="loadings.applyRefund" @click="handleBatch('applyRefund')">
          申请退款</VButton>
        <VButton auth="/purchase/manage/purchase/apply_for_return_of_goods" :loading="loadings.applyReturn" @click="handleBatch('applyReturn')">申请退货
        </VButton>
        <VButton auth="/purchase/manage/purchase/apply_for_payment" :loading="loadings.applyPayment" type="primary" @click="handleBatch('applyPayment')">
          申请付款</VButton>
        <VButton auth="/purchase/manage/purchase/apply_for_debit" :loading="loadings.strike" @click="handleBatch('strike')">申请冲账
        </VButton>
        <VButton auth="/purchase/manage/purchase/updatePurchase" @click="handleBatch('updatePurchase')">调整采购
        </VButton>
        <!-- <VButton auth="/purchase/manage/purchase/addTrack" @click="handleBatch('addTrack')">手动添加快递单号
        </VButton> -->
        <VButton auth="/purchase/manage/purchase/completePurchase" @click="handleBatch('completePurchase')">手动完结
        </VButton>
        <VButton auth="/purchase/manage/purchase/periodApplyPayment" @click="handleBatch('periodApplyPayment')">阿里账期批量申请
        </VButton>
        <VButton auth="/purchase/manage/purchase/updatePrepareType" @click="handleBatch('updatePrepareType')">修改备货方式
        </VButton>
        <UpdateDeclare auth="/purchase/manage/purchase/updateDeclare" mainKey="p_id" :tableRef="tableRef" @success="query" />
        <VImport auth="/purchase/manage/purchase/import_the_tracking_number" :tableRef="tableRef" importUrl="/v1/purchase/purchase/import-tracking-no"
          downloadUrl="https://xc-pms.oss-cn-shenzhen.aliyuncs.com/template/purchase_sku_track_template.xlsx" class="ml-auto" @success="query">导入快递单号</VImport>
        <VAuth auth="/purchase/manage/purchase/exportReport">
          <VExport ref="exportRef" class="absolute -right-full" auth="/purchase/manage/purchase/export-depreciate" mainKey="p_id" name="purchase_price_history" :tableRef="tableRef"
            :exportName="exportName">导出降价报表</VExport>
          <el-dropdown trigger="click" @command="handleExport">
            <el-button>
              <i class="dk-iconfont icon-Upload" />
              导出降价报表<i class="dk-iconfont icon-ArrowDown" />
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item :command="1">采购未税报价维度</el-dropdown-item>
                <el-dropdown-item :command="2">采购未税价维度</el-dropdown-item>
                <el-dropdown-item :command="3">降价报表（完结时间维度）</el-dropdown-item>
                <el-dropdown-item :command="4">降价报表（下单时间维度）</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </VAuth>
        <VAuth auth="/purchase/manage/purchase/export">
          <VExport ref="exportSubRef" class="absolute -right-full" mainKey="p_id" :name="typeSubName" :tableRef="tableRef" :exportName="exportSubName">导出</VExport>
          <el-dropdown trigger="click" @command="handleExportSub">
            <el-button>
              <i class="dk-iconfont icon-Upload" />
              导出<i class="dk-iconfont icon-ArrowDown" />
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item :command="0">默认维度导出</el-dropdown-item>
                <el-dropdown-item :command="1">PO+SKU+入库维度</el-dropdown-item>
                <el-dropdown-item :command="2">PO+SKU+销售维度</el-dropdown-item>
                <el-dropdown-item :command="3">财务导出维度</el-dropdown-item>
                <el-dropdown-item :command="4">PO+发货计划+头程维度</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </VAuth>
        <!-- <VExport auth="/purchase/manage/purchase/export" mainKey="p_id" :name="baseName" :tableRef="tableRef" /> -->
        <VTotal :data="total" :options="[
            { title: '下单SKU数', key: 'sku_num' },
            { title: '下单PCS数', key: 'purchase_pcs' },
            { title: '下单金额', key: 'total_price' },
            { title: '采购单数', key: 'purchase_num' },
            { title: '实际付款金额', key: 'payment_price' },
            { title: '超一天账期未支付', key: 'period_num' },
            ]" />
      </template>
    </VTable>
    <Add ref="addRef" :options="options" />
    <AddTrack ref="addTrackRef" :options="options" @success="query" />
    <Strike ref="strikeRef" :options="options" @success="query" />
    <VLog ref="logRef" />
    <Tracking ref="trackingRef" />
    <UpdatePurchase ref="updatePurchaseRef" @success="query" />
    <ApplyPayment ref="applyPaymentRef" :options="options" isPurchase @success="query" />
    <ApplyRefund ref="applyRefundRef" :options="options" @success="query" />
    <ApplyReturn ref="applyReturnRef" :options="options" @success="query" />
    <Contract ref="contractRef" :options="options" @success="query" />
    <UpdatePrepareType ref="updatePrepareTypeRef" :options="options" @success="query" />
    <Back ref="backRef" :options="options" @success="query" />
    <Pdf ref="pdfRef" />
  </VPage>
</template>

<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import AddTrack from './components/AddTrack'
import ApplyPayment from '@/views/finance/payment/components/ApplyPayment'
import ApplyRefund from './components/ApplyRefund'
import ApplyReturn from './components/ApplyReturn'
import Tracking from './components/Tracking'
import Contract from './components/Contract'
import UpdatePrepareType from './components/UpdatePrepareType'
import Back from './components/Back'
import UpdatePurchase from '@/views/manage/process/components/UpdatePurchase'
import VImport from '@/components/VImport'
import VTotal from '@/components/VTotal'
import UpdateDeclare from '@/components/UpdateDeclare'
import Strike from '@/views/finance/abnormal/components/Add'
import Pdf from '@/views/settings/contract/components/Contract/Pdf'
import { getOption } from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus'
import { resMessage } from '@/utils/res'
import { yesno } from '@/utils'
const baseName = 'purchase'
const tableRef = ref()
const logRef = ref()
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 添加
const addRef = ref()

// 获取options
const options = ref({})
Promise.all([
  getOption({ type: 'payment' }).then(res => res.data),
  getOption({ type: 'refund' }).then(res => res.data),
  getOption({ type: 'return_goods' }).then(res => res.data),
  getOption({ type: baseName }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
  api.getTradeType().then(res => {
    options.value.trade_type = res.data
  })
  api.getPayType().then(res => {
    options.value.pay_type = res.data
  })
})


const exportRef = ref()
const exportName = ref('')
const handleExport = (val) => {
  tableRef?.value.setFormField('export_type', val)
  exportName.value = ['','降价报表-未税报价', '降价报表-未税价', '降价报表-未税价（新）'][val]
  nextTick(() => {
    exportRef?.value.open()
  })
}
const exportSubRef = ref()
const exportSubName = ref('')
const typeSubName = ref()
const handleExportSub = (val) => {
  // tableRef?.value.setFormField('export_type', val)
  typeSubName.value = ['purchase','purchase_sku_inbound','purchase_sku_sale','purchase_sku_finance','purchase_sku_deliver'][val]
  exportSubName.value = ['默认维度导出','PO+SKU+入库维度', 'PO+SKU+销售维度', '财务导出维度','PO+发货计划+头程维度'][val]
  nextTick(() => {
    exportSubRef?.value.open()
  })
}
// 批量操作
const applyPaymentRef = ref()
const applyRefundRef = ref()
const applyReturnRef = ref()
const contractRef = ref()
const strikeRef = ref()
const updatePurchaseRef = ref()
const updatePrepareTypeRef = ref()
const backRef = ref()
const handleBatch = (name) => {
  const selection = tableRef.value.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const p_ids = selection.map(d => d.p_id)
  const showLoading = () => loadings[name] = true
  const closeLoading = () => loadings[name] = false
  const handle = {
    periodApplyPayment: () => {
      showLoading()
      api.periodApplyPayment({ p_ids }).then(res => {
        resMessage({
          res,
          success: () => {
            ElMessage.success('操作成功')
            query()
          }
        })
      }).finally(closeLoading)
    },
    updatePrepareType: () => {
      const { yes, no } = yesno(selection, (d) => d.purchase_status === 1, '采购状态必须是待收货')
      if(!yes) return
      updatePrepareTypeRef?.value.open(yes.map(d => d.p_id))
    },
    completePurchase: () => {
      showLoading()
      api.completePurchase({ p_ids }).then(res => {
        resMessage({
          res,
          success: () => {
            ElMessage.success('操作成功')
            query()
          }
        })
      }).finally(closeLoading)
    },
    updatePurchase: () => {
      updatePurchaseRef?.value.open(p_ids)
    },
    addTrack: () => {
      addTrackRef?.value.open(p_ids)
    },
    back: () => {
      backRef?.value.open(p_ids)
    },
    periodPayment: () => {
      showLoading()
      api.periodPayment({ p_ids }).then(res => {
        const { pay_url } = res.data
        window.open(pay_url, '_blank')
      }).finally(closeLoading)
    },
    applyPayment: async () => {
      showLoading()
      await applyPaymentRef?.value.open(p_ids)
      closeLoading()
    },
    applyRefund: async () => {
      showLoading()
      await applyRefundRef?.value.open(p_ids)
      closeLoading()
    },
    applyReturn: async () => {
      showLoading()
      await applyReturnRef?.value.open(p_ids)
      closeLoading()
    },
    strike: async () => {
      showLoading()
      await strikeRef?.value.open({ p_ids })
      closeLoading()
    },
    contract: () => {
      contractRef?.value.open(p_ids)
    },
    auto_contract: () => {
      showLoading()
      api.autoCreatePurchase({ p_ids }).then(res => {
        const { code, data, message } = res
        const { skip } = data
        if (skip?.length) {
          ElMessageBox.confirm(`${message}是否跳过?`, "提示", {
            type: "error",
            confirmButtonText: "是",
            cancelButtonText: "否",
          }).then((res) => {
            showLoading();
            return api.autoCreatePurchase({ p_ids,skip });
          }).then(res => {
          resMessage({
            res,
            allMsg: true,
            success: () => {
              ElMessage.success("操作成功");
            },
            close: (data) => {},
          });
        })
        .catch(() => {
        })
        .finally(closeLoading);
          return;
        }
        resMessage({
          res,
          allMsg: true,
          success: () => {
            ElMessage.success("操作成功");
          },
          close: (data) => {},
        });
      }).finally(closeLoading)
    }
  }
  handle[name]()
}

// 单行操作
const trackingRef = ref()
const pdfRef = ref()
const addTrackRef = ref()
const handleRow = (name, row, rowIndex) => {
  const { p_id } = row
  const showLoading = () => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = () => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    look: () => {
      addRef?.value.open(p_id)
    },
    addTrack: () => {
      addTrackRef?.value.open({purchase_no: row.purchase_no, sku: row.sku.map(d => ({label: d, value: d}))})
    },
    pdf: () => {
      showLoading('pdf')
      pdfRef?.value.pdf(row, {
        isLook: true,
        closeLoading
      })
    },
    back: () => {
      backRef?.value.open([p_id])
    },
    contract: () => {
      contractRef?.value.open([p_id])
    },
    log: () => {
      logRef?.value.open({ log_type: baseName, p_id })
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  rowConfig: { height: 174 },
  showHeaderOverflow: false,
  rowClassName: ({row}) => row.is_auto_payment === 1 ? 'bg-error' : '',
  formConfig: {
    data: {
      new: { type: 'is_new', value: '' }, // 组合
      input: { type: 'purchase_no', value: '' },
      skuInfo: { type: 'sku', value: '' },
      user: { type: 'purchase_user_id', value: [] },
      warehouse: { type: 'demand_warehouse', value: [] },
      purchase_day: {},
      dateRange: { type: 'create_time', value: [], range: true },
      purchase_audit_status: [4]
    }
  },
  columns: [
    { type: 'checkbox', width: 40, fixed: 'left' },
    {
      width: 180, slots: {
        header: () => <div>采购单</div>,
        default: ({ row }) => <div>
          <VText value={row.purchase_no} type='link' copy onClick={() => handleRow('look', row)}></VText>
          <VText title='类型' value={row.purchase_type_name}></VText>
          <VText title='账期额度支付' value={row.period_pay_status_name}></VText>
          <VText title='是否完结' value={row.is_complete_name}></VText>
          <VText title='完结时间' value={row.complete_time}></VText>
          <VText title='完结类型' value={row.complete_type_name}></VText>
          <VText title='是否有组合sku' value={row.has_combination_name}></VText>
          <VText title='合同' value={row.contract_no} type='button' onClick={() => handleRow('pdf', row)}></VText>
        </div>
      }
    },
    {
      width: 270, slots: {
        header: () => <div>1688信息</div>,
        default: ({ row, rowIndex }) => <div>
          <VMore key={rowIndex}>
            {{
              default: (line) => (row.platform || []).map(d => <div class='w-full'>
                  <VText
                      title='1688订单号'
                      value={d.platform_order_no}
                      type='link'
                      copy
                      onClick={() => {
                          // 打开新窗口
                          window.open(
                              `https://trade.1688.com/order/new_step_order_detail.htm?orderId=${d.platform_order_no}`,
                              '_blank' // 在新窗口打开
                          );
                      }}
                  />
                <VText title='1688订单状态' value={d.order_status_name} />
                <VText title='交易方式' value={d.trade_type_name} />
                <VText title='总金额' value={d.total_price} />
                <VText title='支付方式' value={d.pay_type_name?.join(',')} line={line} />
              </div>)
            }}
          </VMore>
        </div>
      }
    },
    {
      width: 220, slots: {
        header: () => <div>需求信息</div>,
        default: ({ row }) => <div>
          <VText title='订单来源' value={row.order_source_name} />
          <VText title='来源单号' value={row.source_no} />
          <VText title='需求单类型' value={row.demand_form_name} />
          <VText title='需求部门' value={row.department_name} />
          <VText title='需求时间' value={row.demand_time}></VText>
          <VText title='需求仓库' value={row.demand_warehouse_name} />
          <VText title='是否变更' value={row.is_change_name} />
        </div>
      }
    },
    {
      width: 200, slots: {
        header: () => <div>备料信息</div>,
        default: ({ row, rowIndex }) => <VMore key={rowIndex}>
          {{
            default: (line) => (row.plan_data || []).map(d => <div class='w-full'>
          <VText title='备料单号' value={d.plan_no}></VText>
          <VText title='备料数量' value={d.plan_num}></VText>
          <VText title='预计到货时间' value={d.pre_factory_time}></VText>
            </div>)
          }}
        </VMore>
      }
    },
    {
      width: 100, slots: {
        header: () => <div>备货方式<br />发货仓库<br />需求国家</div>,
        default: ({ row }) => <div>
          <VText value={row.prepare_type_name} />
          <VText value={row.destination_warehouse_name} />
          <VText value={row.country_name} />
        </div>
      }
    },
    {
      width: 260, slots: {
        header: () => <div>供应商信息</div>,
        default: ({ row }) => <div>
          <VText title='供应商' value={row.supplier_name}></VText>
          <VText title='供应商id' value={row.supplier_code} />
          <VText title='供应商类型' value={row.purchase_type_name}></VText>
          <VText title='结算方式' value={row.period_name}></VText>
          <VText title='供应商管理人员' value={row.director_id_name}></VText>
        </div>
      }
    },
    {
      width: 120, slots: {
        header: () => <div>产品信息</div>,
        default: ({ row }) => {
          return <div>
            {row.is_cashier_reject === 2 ? <el-tag size='small' type='danger' effect="dark" class='mb-1'>出纳驳回</el-tag> : ''}
            {row.is_accord === 1 ? <el-tag size='small' type='danger' effect="dark" class='mb-1'>{row.is_accord_name}</el-tag> : ''}
            <div class='flex mb-1'>
              {row.is_new === 2 ? <el-tag type="success" effect="dark" size='small' class='mx-1'>新</el-tag> : ''}
              {row.is_special_purchase === 2 ? <el-tag type="danger" size='small' effect="dark">特</el-tag> : ''}
              {row.is_dangerous === 2 ? <svg t="1731396340885" class="-mt-[1px] ml-1" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7755" width="24" height="24"><path d="M426.14784 160.23552l-368.27136 628.736a102.4 102.4 0 0 0 88.35072 154.17344h736.54272a102.4 102.4 0 0 0 88.35072-154.17344l-368.2304-628.736a102.4 102.4 0 0 0-176.7424 0z" fill="#FADE14" p-id="7756"></path><path d="M545.54624 158.96576a61.44 61.44 0 0 1 21.95456 21.99552l368.27136 628.736a61.44 61.44 0 0 1-53.00224 92.48768H146.2272a61.44 61.44 0 0 1-53.00224-92.48768l368.2304-628.736a61.44 61.44 0 0 1 84.09088-21.99552z m-63.73376 28.34432l-2.6624 3.97312-368.27136 628.736a40.96 40.96 0 0 0 30.55616 61.44l4.79232 0.24576h736.54272a40.96 40.96 0 0 0 37.51936-57.42592l-2.17088-4.25984-368.27136-628.736a40.96 40.96 0 0 0-68.03456-3.97312z" fill="#333333" p-id="7757"></path><path d="M450.1504 397.47584a385.26976 385.26976 0 0 1-123.2896 123.2896l19.6608 23.7568c12.288-8.6016 24.576-17.6128 36.864-27.4432v85.1968c-1.6384 67.1744-20.48 119.1936-56.5248 156.8768l23.7568 21.2992c39.3216-44.6464 60.2112-104.0384 62.2592-178.176v-66.7648h277.2992v-28.672h-121.6512c16.384-13.9264 33.1776-31.1296 49.9712-50.7904v-21.2992h-158.1056c6.9632-9.8304 13.5168-20.0704 20.0704-30.72l-30.3104-6.5536z m79.872 109.3632H395.6736c15.9744-14.336 31.1296-29.9008 45.4656-46.6944h138.8544c-16.384 17.6128-33.1776 33.1776-49.9712 46.6944z m73.728 101.5808v38.0928c0 13.1072-5.3248 19.6608-15.9744 19.6608-14.7456 0-31.1296-0.8192-49.152-2.4576l6.9632 27.8528c17.2032 0.8192 34.816 1.2288 52.8384 1.2288 23.3472-0.8192 35.2256-13.5168 35.2256-37.2736v-73.3184h-169.1648v153.6c0 24.576 13.1072 36.864 40.1408 36.864h129.4336c23.3472 0 39.3216-3.6864 47.5136-10.24 9.0112-8.192 15.1552-30.72 18.8416-66.7648l-28.2624-9.0112c-2.4576 23.7568-5.7344 39.7312-10.6496 47.9232-4.5056 6.9632-16.384 10.6496-35.2256 10.6496h-109.7728c-14.7456 0-22.1184-6.5536-22.1184-19.2512v-117.5552h109.3632z" fill="#333333" p-id="7758"></path></svg> : ''}
            </div>
            {row.sku?.slice(0, 2).map(d => <div>{d}</div>) || '-'}
            <VText value='查看详情' type='button' onClick={() => handleRow('look', row)}></VText>
          </div>
        }
      }
    },
    {
      width: 100, slots: {
        header: () => <div>产品图片</div>,
        default: ({ row, rowIndex }) => {
          return <VImage src={row.main_image[0]} list={row.main_image} size='80px' />
        }
      }
    },
    {
      width: 80, slots: {
        header: () => <div>采购状态</div>,
        default: ({ row }) => row.purchase_status_name
      }
    },
    {
      width: 80, slots: {
        header: () => <div>订单状态</div>,
        default: ({ row }) => row.audit_status_name
      }
    },
    {
      width: 180, slots: {
        header: () => <div>数量</div>,
        default: ({ row }) => <div>
          <VText title='需求'>{row.demand_num}</VText>
          <VText title='下单'>{row.purchase_num}</VText>
          <VText title='入库'>{row.inbound_num}</VText>
          <VText title='不良'>{row.reject_num}</VText>
        </div>
      }
    },
    {
      width: 170, slots: {
        header: () => <div>金额</div>,
        default: ({ row }) => <div>
          <VText title='产品总金额' value={row.is_drawback === 2 ? row.total_price_tax : row.total_price}></VText>
          <VText title='运费' value={row.tracking_price}></VText>
          <VText title='优惠' value={row.discount_price}></VText>
          <VText title='采购总金额' value={row.is_drawback === 2 ? row.total_purchase_price_tax : row.total_purchase_price}></VText>
        </div>
      }
    },
    {
      width: 230, slots: {
        header: () => <div>负责人员</div>,
        default: ({ row }) => <div>
          <VText title='采购人员' value={row.purchase_user} line={1}></VText>
          <VText title='下单时间' value={row.create_time}></VText>
          <VText title='审核时间' value={row.audit_time}></VText>
          <VText title='预计到货时间' value={row.expect_delivery_time}></VText>
          <VText title='入库时间' value={row.inbound_time}></VText>
          <VText title='完结时间' value={row.complete_time}></VText>
        </div>
      }
    },
    {
      width: 150, slots: {
        header: () => <div>财务状态</div>,
        default: ({ row }) => <VText value={row.finance_status_name} />
      }
    },
    {
      width: 200, slots: {
        header: () => <div>财务信息</div>,
        default: ({ row, rowIndex }) => {
          const { payment_data } = row
          if (!payment_data?.length) return '-'
          const content = () => payment_data.map(d => <div class='w-full'>
            <VText title='付款单号' value={d.payment_no} type='link'></VText>
            <VText title='申请付款金额' value={d.apply_price}></VText>
            <VText title='实际付款金额' value={d.payment_price}></VText>
            <VText title='付款时间' value={d.payment_time}></VText>
          </div>)

          return <VMore value={content} key={rowIndex} />
        }
      }
    },
    {
      width: 190, slots: {
        header: () => <div>物流信息</div>,
        default: ({ row, rowIndex }) => {
          return <VMore key={rowIndex}>
            {{
              default: (line) => (row.tracking_data || []).map(d => <div class='w-full'>
                <VText title='物流商' value={d.tracking_supplier} />
                <VText title='1688物流状态' value={d.logistics_status} />
                <VText title='单号' type='button' value={d.tracking_no} onClick={() => trackingRef?.value.open(row.tracking_data.map(d => d.tracking_no))}></VText>
                <VText title='揽收时间' value={d.accept_time} />
                <VText title='预估物流时效' value={d.estimate_day} />
                <VText title='预估到达时间' value={d.estimate_arrive_time} />
              </div>)
            }}
          </VMore>
        }
      }
    },
    {
      width: 100, slots: {
        header: () => <div>备注</div>,
        default: ({ row }) => <VText value={row.remark} line={3} />
      }
    },
    {
      title: '操作', width: 136, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          const { detail_purchase_status } = row
          // detail_purchase_status	采购状态: 1等待审核完成 2待供应商负责人确认 3需求驳回 4采购暂停 5待下单 6部分下单 7已全部下单 8取消采购
          return <div class='nm flex flex-wrap gap-1 items-start'>
            <VButton auth="/purchase/manage/purchase/view" link type="primary" onClick={() => handleRow('look', row)}>查看</VButton>
            {row.audit_status !== 6 ? <VButton auth="/purchase/manage/purchase/return" link type="primary" onClick={() => handleRow('back', row)}>退回</VButton> : ''}
            {row.audit_status === 6 || row.contract_no ? '' : <VButton auth="/purchase/manage/purchase/generate_purchase_contract" link type="primary" onClick={() => handleRow('contract', row)}>生成采购合同</VButton>}
            {row.audit_status !== 6 ? <VButton auth="/purchase/manage/purchase/addTrack" link type="primary" onClick={() => handleRow('addTrack', row)}>手动添加快递单号</VButton>:''}
            <VButton auth="/purchase/manage/purchase/logs" link type="primary" onClick={() => handleRow('log', row)}>日志</VButton>
          </div>
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => {
        api.total({ ...form, ...page }).then(res => {
          total.value = res.data
        })
        return api.query({ ...form, ...page })
      }
    }
  }
})
// 数据统计
const total = ref({})

</script>